<template>
  <div class="login-container">
    <i class="top-border"></i>
    <i class="bottom-border"></i>
    <i class="left-border"></i>
    <i class="right-border"></i>
    <span class="title">{{ title }}</span>
    <div class="form-warp">
      <el-form
        class="login-form"
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
      >
        <!-- <div class="title-container">
        <h3 class="title">{{ $t('msg.login.title') }}</h3>
        <lang-select class="lang-select" effect="light"></lang-select>
      </div> -->
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon="user" class="icon-user" />
          </span>
          <el-input
            placeholder="用户名"
            v-model="loginForm.username"
            name="username"
            type="text"
          />
        </el-form-item>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon="password" class="icon-password" />
          </span>
          <el-input
            placeholder="密码"
            v-model="loginForm.password"
            name="password"
            :type="passwordType"
          />
          <span class="show-pwd">
            <svg-icon
              class="icon-eye"
              :icon="passwordType === 'password' ? 'eye' : 'eye-open'"
              @click="onChangePwdType"
            />
          </span>
        </el-form-item>
        <el-row>
          <el-checkbox v-model="checked" label="记住密码" size="large" />
        </el-row>

        <el-button
          type="primary"
          class="login-btn"
          :loading="loading"
          @click="handleLogin"
        >
          <span class="btn-title">{{ $t('msg.login.loginBtn') }}</span>
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { validatePassword } from './rules'
import { useI18n } from 'vue-i18n'
import LangSelect from '@/components/LangSelect'
import { setItem, getItem } from '@/utils/storage'
const title = '平潭地下综合管廊运维管理系统'
const checked = ref(getItem('remberpassword') || false)
//验证规则
const i18n = useI18n()
// 数据源
const loginForm = ref({
  username: 'super-admin',
  password: '12345678'
})
onMounted(() => {
  // 如果勾选了记住密码，则填充密码
  if (getItem('remberpassword')) {
    loginForm.value.password = getItem('curpassword') + ''
  }
})
// 验证规则
const loginRules = ref({
  username: [
    {
      required: true,
      trigger: 'blur',
      message: i18n.t('msg.login.usernameRule')
    }
  ],
  password: [
    {
      required: true,
      trigger: 'blur',
      validator: validatePassword()
    }
  ]
})

// 处理密码框文本显示状态
const passwordType = ref('password')
const onChangePwdType = () => {
  if (passwordType.value === 'password') {
    passwordType.value = 'text'
  } else {
    passwordType.value = 'password'
  }
}

// 处理登录
const loading = ref(false)
const store = useStore()
const router = useRouter()
const loginFormRef = ref(null)

const handleLogin = () => {
  // 1.进行表单校验

  loginFormRef.value.validate((valid) => {
    if (!valid) return
    // 记住密码操作:1.记住勾选状态 2.记住密码
    setItem('remberpassword', checked.value)
    setItem('curpassword', loginForm.value.password)

    // 2.触发登录动作
    loading.value = true

    store
      .dispatch('user/login', loginForm.value)
      .then(() => {
        loading.value = false
        // 3.登录后动作
        router.push('/')
      })
      .catch((err) => {
        console.log(err)
        loading.value = false
      })
  })
}
</script>
<style lang="scss" scoped>
$dark_gray: #889aa4;
$light_gray: #eee;
$cursor: #fff;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url(~@@/images/login_bg.png) no-repeat;
  background-size: 100% 100%;
  overflow: hidden;
  position: relative;

  .top-border {
    width: 1809px;
    height: 88px;
    background: url(~@@/images/login_top_border.png) no-repeat;
    background-size: 100% 100%;
    position: absolute;
    top: 9px;
    left: 61px;
  }
  .bottom-border {
    width: 1811px;
    height: 48px;
    background: url(~@@/images/login_bottom_border.png) no-repeat;
    background-size: 100% 100%;
    position: absolute;
    bottom: 10px;
    left: 61px;
  }
  .left-border {
    width: 45px;
    height: calc(100vh - 88px - 48px);
    background: url(~@@/images/login_left_border.png) no-repeat;
    background-size: 100% 100%;
    position: absolute;
    top: 71px;
    left: 26px;
  }
  .right-border {
    width: 45px;
    height: calc(100vh - 88px - 48px);
    background: url(~@@/images/login_right_border.png) no-repeat;
    background-size: 100% 100%;
    position: absolute;
    top: 67px;
    right: 21px;
  }
  .title {
    font-size: 45px;
    font-family: lianmengqiyilushuaizhengruiheiti;
    font-weight: 400;
    font-style: italic;
    color: #ffffff;
    background: linear-gradient(0deg, #b3bac7 0%, #fbfdff 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    position: absolute;
    top: 63px;
    left: 96px;
    letter-spacing: 5px;
  }
  .form-warp {
    width: 498px;
    height: 516px;
    background: rgba(255, 255, 255, 0);
    border: 1px solid #17cac2;
    box-shadow: -13px 13px 19px 1px rgba(0, 0, 0, 0.4);
    border-radius: 5px;
    margin-left: 40%;
    margin-top: 14%;
    position: relative;
    :deep(.el-checkbox__inner) {
      border: 1px solid #aedcf6b3;
      border-radius: 50%;
      box-sizing: border-box;
      width: 17px;
      height: 17px;
      background-color: #17cac2;
      &::after {
        content: '';
        border: 2px solid #ffffff;
        border-left: 0;
        border-top: 0;
        height: 10px;
        left: 5px;
        position: absolute;
        top: 0px;
        width: 6px;
        transition: transform 0.15s ease-in 50ms;
        transform-origin: center;
      }
    }

    :deep(.el-checkbox__label) {
      font-size: 16px;
      font-family: PingFang SC;
      font-style: italic;
      color: #ffffff;
    }
    .icon-user {
      width: 25px;
      height: 28px;
    }
    .icon-password {
      width: 23px;
      height: 27px;
    }
    .icon-eye {
      width: 29px;
      height: 20px;
      margin-top: 15px;
    }
    .login-form {
      width: 423px;
      position: relative;
      padding: 30px 42px 90px 42px;
      margin: 140px auto;
      overflow: hidden;

      :deep(.el-form-item) {
        border: 0px solid #aedcf6;
        border-bottom: 1px solid rgba(204, 204, 204, 0.8);
        // background: rgba(0, 0, 0, 0.1);
        // border-radius: 5px;
        color: #454545;
        margin-bottom: 30px;
      }

      :deep(.el-input) {
        display: inline-block;
        height: 53px;
        width: 65%;

        input {
          background: transparent;
          border: 0px;
          -webkit-appearance: none;
          border-radius: 0px;
          padding: 12px 5px 12px 15px;
          font-size: 18px;
          font-family: FZLanTingHei-R-GBK;
          font-style: italic;
          color: #ffffff;
          height: 53px;
          caret-color: $cursor;
        }
      }
      :deep(.el-row) {
        margin-top: -10px;
      }
    }
  }

  .login-btn {
    width: 65%;
    height: 45px;
    margin-bottom: 30px;
    margin-left: 15%;
    font-size: 26px;
    font-family: FZLanTingHei-R-GBK;
    font-style: italic;
    color: #f2f2f2;
    margin-top: 20px;
    line-height: 1px;
    background: #17cac2;
    box-shadow: -11px 11px 19px 1px rgba(0, 0, 0, 0.4);
    border-radius: 5px;
    .btn-title {
      letter-spacing: 16px;
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    display: inline-block;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
